﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_Modes;
using Zor_IDAL;
using Zor_Tools;
using System.Data;
using Dapper;
using System.Collections;

namespace Zor_DAL
{
    /// <summary>
    /// 销号管理
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Zor_Disannul_DAL<T> where T : class
    {
        /// <summary>
        /// 消号
        /// </summary>
        /// <param name="accpid">受理单ｉｄ</param>      
        /// <param name="actualtax">税金总和</param>
        /// <param name="dndate">消号时间</param>     
        /// <param name="Jksbh">缴款书编号</param>   
        /// <param name="Sm_Id">汇总主键ｉｄ </param>
        /// <param name="BD_Id">楼宇ｉｄ </param>
        /// 受理状态(0保存,1提交2被退回、3已评议4已评定5、已审核、6已缴税、7、已开票8、已消号、9已汇总)
        /// <returns></returns>
        public static BaseAnswer SummaryDisannul(int Dn_Id, string[] accpid, double Grsds, double Fcs, double actualtax, int dndate, string Jksbh, string[] Bd_Id)
        {
            List<string> lstsql = new List<string>();
            var sws_Id = RequestSession.GetSessionUser().DT_Id; //获取当前登录部门ID(税务所)           
            string now = DateTime.Now.ToString("yyyy-MM-01");  //取到当前的年月
            var Month = TimeHelper.ToTimeStamp(DateTime.Parse(now)); //把当前年月转换成时间戳

            //更新消号表状态
            lstsql.Add(string.Format("update Zor_Disannul set Dn_State=2,Dn_Jksbh='" + Jksbh + "' where Dn_Id=" + Dn_Id + ""));
            //更新受理单状态
            lstsql.Add(string.Format("update [Zor_Acceptance] set Acceptance_State=8 where Acceptance_Id in (" + string.Join(",",accpid) + ")"));
              
            //解析受理单号 ,根据受理单号查出评定的个税、房产税以及楼宇ID ,并存入Hashtable中（键：楼宇ID,值：房产税+个税）
            //Hashtable hsTable = new Hashtable();         
            //string[] accpId_List = accpid[0].Split(',');
            //for (int i = 0; i < accpId_List.Length; i++)
            //{
            //    var accpId = accpId_List[i];
            //    var accpStr = "select zb.Bd_Id,za.Acceptance_Id ,ze.Evaluate_PersonalTax ,ze.Evaluate_PropertyTax from  Zor_Building as zb, Zor_UnitInfor as zu,Zor_Hous as zh,Zor_Lease  as zl,Zor_Acceptance as za,Zor_Review as zr,Zor_Evaluate as ze where zb.Bd_Id=zu.BD_Id and zu.Uim_Id =zh.Uf_Id and zh.Hs_Id =zl.HouseShopId  and zl.Lease_Id =za.Lease_Id and za.Acceptance_Id = zr.Acceptance_Id and zr.Review_Id =ze.Review_Id and za.Acceptance_Id =" + accpId + "";
            //    dynamic accpList = DapperQuery<T>.DoQueryT(accpStr, null);
            //    hsTable.Add(accpList.Bd_Id, accpList.Evaluate_PersonalTax + "," + accpList.Evaluate_PropertyTax);
            //}            
            //string[] bd_id_List = Bd_Id[0].Remove(Bd_Id[0].Length-1,1).Split(',');


            string[] accpId_List = accpid[0].Split(',');

            for (int i = 0; i < accpId_List.Length; i++)
            {
               // int bitem =Convert.ToInt32(bd_id_List[i]);
                var accpId = accpId_List[i];
                var accpStr = "select zb.Bd_Id,za.Acceptance_Id ,ze.Evaluate_PersonalTax ,ze.Evaluate_PropertyTax from  Zor_Building as zb, Zor_UnitInfor as zu,Zor_Hous as zh,Zor_Lease  as zl,Zor_Acceptance as za,Zor_Review as zr,Zor_Evaluate as ze where zb.Bd_Id=zu.BD_Id and zu.Uim_Id =zh.Uf_Id and zh.Hs_Id =zl.HouseShopId  and zl.Lease_Id =za.Lease_Id and za.Acceptance_Id = zr.Acceptance_Id and zr.Review_Id =ze.Review_Id and za.Acceptance_Id =" + accpId + "";
                dynamic accpList = DapperQuery<T>.DoQueryT(accpStr, null);

                int bitem = accpList.Bd_Id;
                Grsds = accpList.Evaluate_PersonalTax;
                Fcs = accpList.Evaluate_PropertyTax;


                string SqID = "select Dt_Id from Zor_Building where Bd_Id = " + bitem + "";
                List<dynamic> Dt_IdLst = DapperQuery<dynamic>.DoQueryList(SqID, null); //查询楼宇所在社区ID
                string jdbID = "select Dt_ParentId from Sys_Department where Dt_Id = " + Dt_IdLst[0].Dt_Id + "";
                List<dynamic> StreeIDLst = DapperQuery<dynamic>.DoQueryList(jdbID, null); //查询社区对应的街道办ID

                //Grsds = Convert.ToDouble(hsTable[bitem].ToString().Split(',')[0]);
                //Fcs = Convert.ToDouble(hsTable[bitem].ToString().Split(',')[1]);

                //1.-街道办月份统计
                string JDBIdStr = "select Stree_ID from Zor_PayTaxeCount where [Type]=2 and Pt_Date=" + Month + " and Stree_ID =" + StreeIDLst[0].Dt_ParentId + " ";
                List<dynamic> IsStree_ID = DapperQuery<dynamic>.DoQueryList(JDBIdStr, null); //对比街道办ID
                if (IsStree_ID.Count == 0)
                {
                    //没有街道办月份统计（新增）
                  string StreeStr = "insert into [Zor_PayTaxeCount](ANumber,PNumber,Fcs,Grsds,Dt_Id,Stree_ID,Bd_Id,Type,Pt_Date) values(1,1," + Fcs + "," + Grsds + "," + Dt_IdLst[0].Dt_Id + "," + StreeIDLst[0].Dt_ParentId + "," + bitem + ",2," + Month + ")";
                  DapperQuery<dynamic>.DoQueryList(StreeStr, null); 
                }
                else
                {
                    //修改街道办月统计
                    lstsql.Add(string.Format("update Zor_PayTaxeCount set ANumber= ANumber+ 1, PNumber = PNumber +1 ,Grsds=Grsds+" + Grsds  + ",Fcs = Fcs+" + Fcs  + " where [Type]=2  and  Pt_Date=" + Month + " and Stree_ID  = " + StreeIDLst[0].Dt_ParentId + " "));
                }
                //b-修改街道办总统计表         
                lstsql.Add(string.Format("update Zor_PayTaxeCount set PNumber = PNumber +1, Grsds=Grsds+" + Grsds  + ",Fcs = Fcs+" + Fcs  + " where [Type]=2 and Pt_Date=0 and Stree_Id = " + StreeIDLst[0].Dt_ParentId + ""));


                //2.a- 社区月份统计（有当前月，修改月份统计，没有当前月新增月份统计）
                string SQIdStr = "select Dt_Id from Zor_PayTaxeCount where [Type]=1 and Pt_Date=" + Month + " and Dt_Id =" + Dt_IdLst[0].Dt_Id + "";
                List<dynamic> IsDt_Id = DapperQuery<dynamic>.DoQueryList(SQIdStr, null); //对比社区ID
                if(IsDt_Id.Count == 0) 
                {
                    //新增社区月统计
                  string DtStr = "insert into [Zor_PayTaxeCount](ANumber,PNumber,Fcs,Grsds,Dt_Id,Stree_ID,Bd_Id,Type,Pt_Date) values(1,1," + Fcs  + "," + Grsds  + "," + Dt_IdLst[0].Dt_Id + "," + StreeIDLst[0].Dt_ParentId + "," + bitem + ",1," + Month + ")";
                  DapperQuery<dynamic>.DoQueryList(DtStr, null); 
                }
                else
                {
                    //修改社区月统计
                    lstsql.Add(string.Format("update Zor_PayTaxeCount set ANumber= ANumber+ 1, PNumber = PNumber +1 ,Grsds=Grsds+" + Grsds  + ",Fcs = Fcs+" + Fcs  + " where [Type]=1 and Pt_Date=" + Month + "  and Dt_Id = " + Dt_IdLst[0].Dt_Id + " "));
                }
                //  b- 修改社区总统计表
                lstsql.Add(string.Format("update Zor_PayTaxeCount set PNumber = PNumber +1 ,Grsds=Grsds+" + Grsds  + ",Fcs = Fcs+" + Fcs  + " where [Type]=1 and Pt_Date = 0 and   Dt_Id = " + Dt_IdLst[0].Dt_Id + " "));


                //3.a-楼宇月统计 
                string BdIdStr = "select Bd_Id from Zor_PayTaxeCount where [Type]=3 and Pt_Date=" + Month + " and Bd_Id =" + bitem + "";
                List<dynamic> IsBd_ID = DapperQuery<dynamic>.DoQueryList(BdIdStr, null); //对比楼宇ID
                if (IsBd_ID.Count == 0)
                {
                    //新增楼宇月统计表
                  string bdStr = "insert into [Zor_PayTaxeCount](ANumber,PNumber,Fcs,Grsds,Dt_Id,Stree_ID,Bd_Id,Type,Pt_Date) values(1,1," + Fcs  + "," + Grsds + "," + Dt_IdLst[0].Dt_Id + "," + StreeIDLst[0].Dt_ParentId + "," + bitem + ",3," + Month + ")";
                  DapperQuery<dynamic>.DoQueryList(bdStr, null); 
                }
                else
                {
                    lstsql.Add(string.Format("update Zor_PayTaxeCount set ANumber= ANumber+ 1, PNumber = PNumber +1 ,Grsds=Grsds+" + Grsds  + ",Fcs = Fcs+" + Fcs  + " where [Type]=3  and  Pt_Date=" + Month + " and  Bd_Id = " + bitem + ""));
                }
                    //b-修改楼宇总统计表   
                lstsql.Add(string.Format("update Zor_PayTaxeCount set PNumber =PNumber + 1,Grsds=Grsds+" + Grsds  + ",Fcs = Fcs+" + Fcs + " where [Type]=3 and  Pt_Date=0 and Bd_Id =" + bitem + ""));
            }

            BaseAnswer bar = DapperTransaction.DoTransaction(lstsql);
            return bar;      
        }
    }
}
